package com.baizhi.controller;

import com.baizhi.entity.Department;
import com.baizhi.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("selectDepartmentByPage")
    @ResponseBody
    public Map selectDepartmentByPage(int page, int rows, String name){
        Map map=new HashMap();
        int total = departmentService.selectTotal(name);
        map.put("total",total);
        List<Department> list = departmentService.selectDepartmentByPage(page, rows, name);
        map.put("rows",list);
        return map;
    }

    @RequestMapping("add")
    @ResponseBody
    public Map add(Department department) {
        Map map = new HashMap();

        try {
            departmentService.insert(department);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @RequestMapping("update")
    @ResponseBody
    public Map update(Department department) {
        Map map = new HashMap();

        System.out.println(department);

        try {
            departmentService.update(department);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

    @RequestMapping("multiDelete")
    @ResponseBody
    public Map multiDelete(int ids[]) {
        Map map = new HashMap();
        System.out.println();
        try {
            departmentService.delete(ids);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("deleteOne")
    @ResponseBody
    public String deleteOne(int id) {
        departmentService.deleteOne(id);
        return "t_department";
    }

}
